﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using root.Proxies;
using System.Threading;
using SaaSKit.Taskr.Service;
using root.Models;
using Apprenda.SaaSGrid;
using System.ServiceModel;

namespace root.Controllers
{
    public class TasksController : BaseController
    {

        //
        // GET: /Tasks/
        public ActionResult Index()
        {
            return View();
        }

        // Action: /Tasks/ListAllTasks
        [HttpGet]        
        public JsonResult ListAllTasks()
        {            
            try
            {
                CoreServiceProxy proxy = new CoreServiceProxy();
                var dtos = proxy.ListAllTasks();
                var tasks = new List<Task>();
                foreach (TaskDTO dto in dtos)
                {
                    Task task = new Task();
                    task.MapFrom(dto);
                    tasks.Add(task);
                }
                return Json(tasks, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {
                throw e;
            }            
        }

        // Action: /Tasks/ListUserTasks
        [HttpGet]
        public JsonResult ListUserTasks()
        {
            CoreServiceProxy proxy = new CoreServiceProxy();
            var dtos = proxy.ListTasks(UserContext.Instance.CurrentUser.Id);
            var tasks = new List<Task>();
            foreach (TaskDTO dto in dtos)
            {
                Task task = new Task();
                task.MapFrom(dto);
                tasks.Add(task);
            }
            return Json(tasks, JsonRequestBehavior.AllowGet);
        }

        // Action: /Tasks/ListUserTasks
        [HttpGet]
        public JsonResult ListTaskActivity(Guid taskId)
        {
            CoreServiceProxy proxy = new CoreServiceProxy();
            TaskDTO task = proxy.GetTask(taskId);
            return Json(task.ActivityHistory, JsonRequestBehavior.AllowGet);
        }

        // Action: /Tasks/AddTask
        public void AddTask(TaskDTO task)
        {
            
            if (task.AssigneeId == null)
            {
                task.AssigneeId = UserContext.Instance.CurrentUser.Id;
            }

            CoreServiceProxy proxy = new CoreServiceProxy();
            proxy.CreateTask(task);

        }

        // Action: /Tasks/DeleteTask
        public void DeleteTask(Guid taskId)
        {
            CoreServiceProxy proxy = new CoreServiceProxy();
            proxy.DeleteTask(taskId);
        }

        // Action: /Tasks/CompleteTask
        public void CompleteTask(Guid taskId)
        {
            // Note that we are retrieving the task first because the service
            // improperly attempts to update unset properties during the call
            // to UpdateTask, so we need to send the whole object for now.
            CoreServiceProxy proxy = new CoreServiceProxy();
            TaskDTO dto = proxy.GetTask(taskId);
            dto.CompletedDate = DateTime.Now;
            proxy.UpdateTask(dto);
        }

        // Action: /Tasks/ReopenTask
        public void ReopenTask(Guid taskId)
        {
            // Note that we are retrieving the task first because the service
            // improperly attempts to update unset properties during the call
            // to UpdateTask, so we need to send the whole object for now.
            CoreServiceProxy proxy = new CoreServiceProxy();
            TaskDTO dto = proxy.GetTask(taskId);
            dto.CompletedDate = null;
            proxy.UpdateTask(dto);
        }

    }
}
